<?php
require_once dirname(__FILE__) . '/../fblogin/FBLoginModel.php';
require_once dirname(__FILE__) . '/../common/IController.php';
require_once dirname(__FILE__) . '/EventModel.php';
require_once dirname(__FILE__) . '/EventView.php';
require_once dirname(__FILE__) . '/VisitorEvent.php';
require_once dirname(__FILE__) . '/StaffEvent.php';
require_once dirname(__FILE__) . '/../common/BaseController.php';

class EventController extends BaseController implements IController {
	
	public function __construct(Database $database, FBLoginModel $loginModel, NavigationView $navigationView) {
	    parent::__construct($database, $loginModel, $navigationView);
	}

	/**
	* Låter registrerade användare anmäla intresse för att jobba under event, samt visar information
	* om eventen för icke registrerade användare
	*
	* @return String HTML
	*/
	public function doControl() {
		$em = new EventModel($this->database);
		$ev = new EventView();
		$html = '';

		$events = $em->getEvents();

		if ($this->loginModel->getUserRole() & UsersModel::USER_ROLE) {
			$em = new EventModel($this->database);
			$ev = new EventView();

			if ($ev->triedToApplyToEvent()) {
				$eventStaff = $ev->getEventApplicationInput();
				$user = $this->loginModel->getLoggedInUserFromDb();

				$eventStaff->username = $user->username;
				// detta värde behöver inte skickas med vid skapande av event
				$eventStaff->visitorEventStart = null;
				$eventStaff->confirmed = false;

				$sm = new StaffModel($this->database);
				$sm->submitStaffApplication($eventStaff);

			}

			$html .= $ev->getStaffEventsMarkup($events);

		} else {
			$html .= $ev->getEventsMarkup($events);
		}

		return $html;		
	}	
}
